<?php
/**
 * 操作控制器
 *
 * 构造函数中初始化业务逻辑所需的数据
 *
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2011 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
abstract class UE_Controller_Action extends UE_Controller_Abstract {
	/**
	 * 视图对象
	 *
	 * @var UE_Template
	 */
	public $tpl = null;

	/**
	 * 模板id
	 *
	 * @var string
	 */
	public $tplId = 'default';

	public function init() {}

	final public function __construct() {
		$this->app      = $_GET['app'];
		$this->ctl      = $_GET['ctl'];
		$this->act      = $_GET['act'];
		$this->id       = $_GET['id'];
		$this->appDir   = APPS_DIR . '/' . $_GET['app'];
		
		
		// 载入当前用户地区使用的默认的语言包
		$this->_locale = UE_Lang::getLocale();
		
		UE_Lang::add($this->app);
		
		// 初始化视图
		$this->tpl = new UE_Template();
		
		$this->tpl->setTplId($this->_locale);		
		$this->tpl->setStorage(cfg('compile_storage'));

		$this->tpl->forceCompile = cfg('force_compile');
		$this->tpl->compiledPath = cfg('compiled_path');
		$this->tpl->mergeCompile = cfg('merge_compile');
		$this->tpl->tplPath      = 'themes';
		$this->tpl->tpl          = cfg('tpl');
	
		$this->tpl->assign('app',        $_GET['app']);
		$this->tpl->assign('ctl',        $_GET['ctl']);
		$this->tpl->assign('act',        $_GET['act']);
		$this->tpl->assign('id',         $_GET['id']);
		
		$this->tpl->assign('site',       UE::site());
		$this->tpl->assign('ctx',        UE::context());
		$this->tpl->assign('base',       UE::base());
		
		// 在配置文件中固定一套相应的模板
		$this->tpl->assign('themesPath',         UE::context() . 'themes/');
		$this->tpl->assign('themePath',          UE::context() . 'themes/' . cfg('tpl') . '/'); 
		$this->tpl->assign('defaultThemePath',   UE::context() . 'themes/default/'); 
		
		$this->tpl->assign('stylePath',          $this->tpl->themePath . 'style/'.cfg('style').'/');
		$this->tpl->assign('defaultStylePath',   UE::context() . 'themes/default/style/default');
		$this->tpl->assign('url',                UE::url());
		$this->tpl->assign('urlEncoded',         UE::urlEncoded());
		$this->tpl->assign('home',               UE_Url::make(cfg('default_app') . '/'.cfg('default_ctl'). '/'.cfg('default_act')));
		
		//*
		// options
		
		$this->tpl->assign('siteName',           OptionModel::getOption('SITE_NAME'));
		
		$this->tpl->assign('cats', CatModel::getCats('', 0, false));
		
		$options = OptionModel::loadOpts();
		foreach($options as $optionKey => $option) {
			$this->tpl->assign($optionKey, $option);
		}

		$this->tpl->assign('apps',              AppModel::getApps());
		$this->tpl->assign('acts',              AppModel::getActs());
		$this->tpl->assign('acls',              AppModel::getAcls());
		$this->tpl->assign('keywords',          OptionModel::getOption('SITE_KEYWORD'));
		$this->tpl->assign('description',       OptionModel::getOption('SITE_DESCRIPTION'));
		
		$this->tpl->assign('embedHeader', OptionModel::getOption('EMBED_HEADER'));
		$this->tpl->assign('embedFooter', OptionModel::getOption('EMBED_FOOTER'));
		//*/
	}

	public function __call($fun, $param) {
		// 当然这里要做一个页面
		$msg = sprintf(lang('ue.action.404'), UE::url());
		UE_Msgs::setErr($msg);
		$this->_404();
		return;
	}
	
	/**
	 * 没有权限访问
	 *
	 */
	protected function _403() {
		$this->tpl->apply('403');
	}
	
	/**
	 * 该页面不存在
	 *
	 */
	protected function _404() {
	    //UE_Header::setStatus(404);
		$this->tpl->apply('404');
	}
	
	public function messageAction() {
		$this->showMessage();
	}
	
	
	public function showMessage($tpl = 'message') {
	    $tpl = $tpl ? $tpl : 'message';	
		$this->tpl->assign('title', '提示信息');
		$this->tpl->setErr($this->getErrs());
		$this->tpl->apply($tpl);
	}
}
